/*
 * Copyright Thoughtworks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import m from "mithril";
import * as Icons from "views/components/icons";
import { i18n } from "helpers/i18n";
import styles from "./index.scss";

import { MithrilViewComponent } from "jsx/mithril-component";
import { MaintenanceModeInfo, RunningSystem } from "models/maintenance_mode/types";

interface Attrs {
  maintenanceModeInfo: MaintenanceModeInfo;
}

class InformationWhenNotInMaintenanceMode extends MithrilViewComponent<Attrs> {
  view(vnode: m.Vnode<Attrs>) {
    return (
      <div data-test-id="info-when-not-in-maintenance-mode">
        <h3 data-test-id="info-when-not-in-maintenance-mode-header">
          {i18n.tSyncForPage("admin.maintenance.effects.title", "admin-maintenance")}
        </h3>
        <ul class={styles.gocdSubSystemInfo}>
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId={"stop-material"}
            text={i18n.tSyncForPage("admin.maintenance.effects.stop.material", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId="stop-config-repo"
            text={i18n.tSyncForPage("admin.maintenance.effects.stop.config.repo", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId="stop-pipeline-scheduling"
            text={i18n.tSyncForPage("admin.maintenance.effects.stop.scheduling", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId="stop-work-assignment"
            text={"Stop the agent subsystem, so that no agents can pick up work if they’re idle."}
          />
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId="stop-manual-trigger"
            text={i18n.tSyncForPage("admin.maintenance.effects.stop.manual.trigger", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId="stop-config-changes"
            text={i18n.tSyncForPage("admin.maintenance.effects.stop.config.changes", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            isReadOnly={true}
            dataTestId="stop-db-changes"
            text={i18n.tSyncForPage("admin.maintenance.effects.stop.db.changes", "admin-maintenance")}
          />
        </ul>
      </div>
    );
  }
}

interface RunningSystemAttrs {
  isReadOnly?: boolean;
  inProgress?: boolean;
  dataTestId: string;
  text: string;
}

class SubsystemInfoWithIconWidget extends MithrilViewComponent<RunningSystemAttrs> {
  view(vnode: m.Vnode<RunningSystemAttrs>) {
    let icon = <Icons.Minus iconOnly={true} />;

    if (!vnode.attrs.isReadOnly) {
      icon = vnode.attrs.inProgress ? <Icons.Spinner iconOnly={true} /> : <Icons.Check iconOnly={true} />;
    }

    return (
      <li data-test-id={vnode.attrs.dataTestId} class={styles.runningSystem}>
        {icon}
        <span class={styles.runningSystemText}>{vnode.attrs.text}</span>
      </li>
    );
  }
}

class InformationWhenInMaintenanceMode extends MithrilViewComponent<Attrs> {
  view(vnode: m.Vnode<Attrs>) {
    const runningSystem = vnode.attrs.maintenanceModeInfo.runningSystem as RunningSystem;
    const mduRunningSystem =
      runningSystem.materialUpdateInProgress.count() === 0 ? (
        <SubsystemInfoWithIconWidget
          inProgress={false}
          dataTestId={"mdu-stopped"}
          text={i18n.tSyncForPage("admin.maintenance.subsystem.mdu.stopped", "admin-maintenance")}
        />
      ) : (
        <SubsystemInfoWithIconWidget
          inProgress={true}
          dataTestId={"mdu-in-progress"}
          text={i18n.tSyncForPage("admin.maintenance.subsystem.mdu.in.progress", "admin-maintenance")}
        />
      );

    const buildingJobsSystem =
      runningSystem.buildingJobsGroupedByStages.length === 0 ? (
        <SubsystemInfoWithIconWidget
          inProgress={false}
          dataTestId={"scheduling-system-stopped"}
          text={i18n.tSyncForPage("admin.maintenance.subsystem.scheduling.stopped", "admin-maintenance")}
        />
      ) : (
        <SubsystemInfoWithIconWidget
          inProgress={true}
          dataTestId={"scheduling-system-in-progress"}
          text={i18n.tSyncForPage("admin.maintenance.subsystem.scheduling.in.progress", "admin-maintenance")}
        />
      );

    return (
      <div data-test-id="info-when-not-in-maintenance-mode">
        <ul class={styles.runningSubSystem} data-test-id="running-sub-systems">
          {mduRunningSystem}
          <SubsystemInfoWithIconWidget
            inProgress={false}
            dataTestId={"config-repo-polling-stopped"}
            text={i18n.tSyncForPage("admin.maintenance.subsystem.config.repo.stopped", "admin-maintenance")}
          />
          {buildingJobsSystem}
          <SubsystemInfoWithIconWidget
            inProgress={false}
            dataTestId={"agent-subsystem-stopped"}
            text={i18n.tSyncForPage("admin.maintenance.subsystem.agent.stopped", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            inProgress={false}
            dataTestId={"manual-trigger-stopped"}
            text={i18n.tSyncForPage("admin.maintenance.subsystem.manual.trigger.stopped", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            inProgress={false}
            dataTestId={"config-changes-stopped"}
            text={i18n.tSyncForPage("admin.maintenance.subsystem.config.changes.stopped", "admin-maintenance")}
          />
          <SubsystemInfoWithIconWidget
            inProgress={false}
            dataTestId={"db-changes-stopped"}
            text={i18n.tSyncForPage("admin.maintenance.subsystem.db.changes.stopped", "admin-maintenance")}
          />
        </ul>
      </div>
    );
  }
}

export class DisabledSubsystemsWidget extends MithrilViewComponent<Attrs> {
  oninit() {
    // Epic-10 优化：设置当前页面上下文，自动预加载翻译
    i18n.setCurrentPage("admin-maintenance");
  }

  view(vnode: m.Vnode<Attrs>) {
    return vnode.attrs.maintenanceModeInfo.maintenanceModeState() ? (
      <InformationWhenInMaintenanceMode maintenanceModeInfo={vnode.attrs.maintenanceModeInfo} />
    ) : (
      <InformationWhenNotInMaintenanceMode maintenanceModeInfo={vnode.attrs.maintenanceModeInfo} />
    );
  }
}
